package com.example.demo.utils.encrypt;

import com.example.demo.utils.StringUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

public class DESUtil
{

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "DES";

    /**
     * 加解密算法/工作模式/填充方式
     */
    //private static final String ALGORITHM_MODE_PADDING = "DES/CBC/PKCS5Padding";
    private static final String ALGORITHM_MODE_PADDING = "DES/ECB/PKCS7Padding";

    /**
     * 签名密钥
     */
    private static final String SIGNATURE_KEY = "~!@#$%^&*(+-*/=).";

    /**
     * DES根据 默认的key 进行加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptData(String data) throws Exception
    {

        if (data == null)
        {
            return null;
        }

        byte[] bt = encrypt(data.getBytes(), getMd5EncryptKey().getBytes());

        return new BASE64Encoder().encode(bt);
    }

    /**
     * DES根据 KEY 进行加密
     *
     * @param data
     * @param key  MD5加密过的Key
     * @return
     * @throws Exception
     */
    public static String encryptData(String data, String key) throws Exception
    {

        if (data == null)
        {
            return null;
        }

        byte[] bt = encrypt(data.getBytes(), StringUtil.isEmpty(key) ? getMd5EncryptKey().getBytes() : key.getBytes());

        return new BASE64Encoder().encode(bt);
    }

    /**
     * DES根据 默认的key 进行解密
     *
     * @param base64Data
     * @return
     * @throws Exception
     */
    public static String decryptData(String base64Data) throws Exception
    {
        if (base64Data == null)
        {
            return null;
        }

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(base64Data);

        byte[] bt = decrypt(buf, getMd5EncryptKey().getBytes());

        return new String(bt, "UTF-8");
    }

    /**
     * DES根据 key 进行解密
     *
     * @param base64Data
     * @param key        MD5加密过的Key
     * @return
     * @throws Exception
     */
    public static String decryptData(String base64Data, String key) throws Exception
    {
        if (base64Data == null)
        {
            return null;
        }

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(base64Data);

        byte[] bt = decrypt(buf, StringUtil.isEmpty(key) ? getMd5EncryptKey().getBytes() : key.getBytes());

        return new String(bt, "UTF-8");
    }

    /**
     * DES加密
     *
     * @param data
     * @param key  key的byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception
    {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }


    /**
     * DES解密
     *
     * @param data
     * @param key  key的byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception
    {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    private static String getMd5EncryptKey()
    {
        return MD5Util.MD5Encode(SIGNATURE_KEY, "UTF-8").toLowerCase();
    }
}
